<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_order;

use Plugin\Attractions\Commerce\Model\Commerce_order\CommerceOrder;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 订单 Repository类
 */
class CommerceOrderRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceOrder $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 订单号
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商户ID
        if (isset($params['merchant_id']) && !empty($params['merchant_id'])) {
            $query->where('merchant_id', '=', $params['merchant_id']);
        }

        if (isset($params['customer_id']) && !empty($params['customer_id'])) {
            $query->where('customer_id', '=', $params['customer_id']);
        }

        // 客户姓名
        if (isset($params['customer_name']) && !empty($params['customer_name'])) {
            $query->where('customer_name', 'like', '%'.$params['customer_name'].'%');
        }

        // 客户电话
        if (isset($params['customer_phone']) && !empty($params['customer_phone'])) {
            $query->where('customer_phone', 'like', '%'.$params['customer_phone'].'%');
        }

        // 客户地址
        if (isset($params['customer_address']) && !empty($params['customer_address'])) {
            $query->where('customer_address', 'like', '%'.$params['customer_address'].'%');
        }

        // 订单来源：1-线上下单，2-现场订单
        if (isset($params['order_source']) && !empty($params['order_source'])) {
            $query->where('order_source', '=', $params['order_source']);
        }

        // 订单总金额
        if (isset($params['total_amount']) && !empty($params['total_amount'])) {
            $query->where('total_amount', '=', $params['total_amount']);
        }

        // 实付金额
        if (isset($params['actual_amount']) && !empty($params['actual_amount'])) {
            $query->where('actual_amount', '=', $params['actual_amount']);
        }

        // 付款方式1-银行转账,2-微信支付,3-支付宝支付,4-现金,5-其他付款方式
        if (isset($params['payment_method']) && !empty($params['payment_method'])) {
            $query->where('payment_method', '=', $params['payment_method']);
        }

        // 支付时间
        if (isset($params['payment_time']) && !empty($params['payment_time']) && is_array($params['payment_time']) && count($params['payment_time']) == 2) {
            $query->whereBetween(
                'payment_time',
                [ $params['payment_time'][0], $params['payment_time'][1] ]
            );
        }

        // 发货时间
        if (isset($params['delivery_time']) && !empty($params['delivery_time']) && is_array($params['delivery_time']) && count($params['delivery_time']) == 2) {
            $query->whereBetween(
                'delivery_time',
                [ $params['delivery_time'][0], $params['delivery_time'][1] ]
            );
        }

        // 快递公司
        if (isset($params['express_company']) && !empty($params['express_company'])) {
            $query->where('express_company', 'like', '%'.$params['express_company'].'%');
        }

        // 快递单号
        if (isset($params['express_id']) && !empty($params['express_id'])) {
            $query->where('express_id', 'like', '%'.$params['express_id'].'%');
        }

        // 快递信息
        if (isset($params['express_info']) && !empty($params['express_info'])) {
            $query->where('express_info', '=', $params['express_info']);
        }

        // 订单状态：1-未支付，2-已支付，3-已发货，4-已到货，5-已完成，6-已取消
        if (isset($params['order_status']) && !empty($params['order_status'])) {
            $query->where('order_status', '=', $params['order_status']);
        }

        // 取消原因
        if (isset($params['cancel_reason']) && !empty($params['cancel_reason'])) {
            $query->where('cancel_reason', 'like', '%'.$params['cancel_reason'].'%');
        }

        // 取消时间
        if (isset($params['cancel_time']) && !empty($params['cancel_time']) && is_array($params['cancel_time']) && count($params['cancel_time']) == 2) {
            $query->whereBetween(
                'cancel_time',
                [ $params['cancel_time'][0], $params['cancel_time'][1] ]
            );
        }

        // 完成时间
        if (isset($params['finish_time']) && !empty($params['finish_time']) && is_array($params['finish_time']) && count($params['finish_time']) == 2) {
            $query->whereBetween(
                'finish_time',
                [ $params['finish_time'][0], $params['finish_time'][1] ]
            );
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}